package 并发编程02.volatile关键字案例;

import java.util.HashSet;

//变量的可见性
public class VolatileVisible {
    volatile boolean ready=true;
    private final static int SIZE=1000;

    public static void main(String[] args) throws InterruptedException {
        VolatileVisible[] vv=new VolatileVisible[SIZE];
        for (int i=0;i<SIZE;i++) {
            (vv[i]=new VolatileVisible()).test(i);
        }
        System.out.println("---------");

    }

    public void test(int i) throws InterruptedException {
        Thread t2=new Thread(){
            public void run(){
                System.out.println(""+ready+" "+i);
                while (ready){};
                System.out.println("this is end!"+i);
            }
        };
        Thread t1=new Thread(){
            public void run(){
                ready=false;
                System.out.println(i+" change false");
            }
        };
        t2.start();
//        Thread.yield();
        t1.start();
        t1.join();
        t2.join();
    }
}
